package com.fisher.IdempotentDemo.controller;

import cn.hutool.core.util.StrUtil;
import com.fisher.IdempotentDemo.annotation.AutoIdempotent;
import com.fisher.IdempotentDemo.exception.ServiceException;
import com.fisher.IdempotentDemo.service.TestService;
import com.fisher.IdempotentDemo.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：fisher wu
 * @date ：Created in 2021/11/23 11:30 上午
 * @description：业务controller
 */
@RestController
public class BusinessController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private TestService testService;


    @PostMapping("/get/token")
    public String getToken() {
        String token = tokenService.createToken();
        if (StrUtil.isNotEmpty(token)) {
            return token;
        }
        return "";
    }

    @AutoIdempotent
    @PostMapping("/test/idempotence")
    public String testIdempotence() {
        try {
            testService.testIdempotence();
        } catch (Exception e) {
            return e.getMessage();
        }
        return "200";
    }
}
